<?php
namespace common\models;


use yii\db\ActiveRecord;

class UserCollection extends ActiveRecord
{

    public static function tableName()
    {
        return 'user_collection';
    }

    public function rules()
    {
        return [
            [['id','user_id','list_id','type','status','create_time','update_time'],'integer'],

        ];
    }

    public function attributeLabels()
    {
        return [
            'id	'=>'id',
            'user_id'=>'用户id',
            'list_id'=>'内容id',
            'type'=>'类型',
            'status'=>'状态类型',
            'create_time'=>'创建时间',
            'update_time'=>'更新时间'
        ];
    }

    /**
     * 添加收藏
     * @param $data
     * @return bool
     */
    public static function addCollection($data){
        //先查找是否有
        $row = self::find()->where([
            'user_id'=>$data['user_id'],
            'list_id'=>$data['list_id'],
            'type'=>$data['type'],
            'status'=>$data['status'],
            ])->one();
        if(!empty($row)){
            $row['update_time'] = $data['update_time'];
            return $row->save();
        }
        $Model= new self();

        $Model->setAttributes($data);
        if($Model->save()){
            return true;
        }
        return false;


    }
}